package com.lq.learningTest.AnnotationTest;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ApplicationContext {
    // 定义一个线程安全的ConcurrentHashMap作为IOC容器，key是Class，value是实例
    private final static Map<Class<?>,Object> CONTEXT = new ConcurrentHashMap<>(8);

    // 将对象以Class为key缓存进容器
    public static void addSingleton(Class<?> clazz,Object entity){
        ApplicationContext.CONTEXT.put(clazz,entity);
    }

    // 从容器中取出指定Class类型的实例并进行类型转换
    public static  <T> T getSingleton(Class<T> clazz){
        return (T)ApplicationContext.CONTEXT.get(clazz);
    }
}
